var jude = Require(path.join(__dirname, 'verifty.js'));
var textarea = function (options, layout) {
    this.options = {
        size: 'normal',
        jude: {
            name: '',
            rules: []
        },
        readonly: false,
        placeholder: '',
        changeEvent: function (e) { }
    };
    this.layout = layout;
    this.options = $.extend({}, this.options, options);
    this.element = $('<textarea spellcheck="false" class="form_textarea ' + this.options.size + '"></textarea>');
    this.html = this.element;
    this.placeHolder();
    this.readonly(this.options.readonly);
    this.objectName = null;
    // this.changeEvent = this.options.changeEvent;
    var _this = this;
    this.element.on('input propertychange', function () {
        _this.change();
        _this.options.changeEvent(_this);
    });

    this.element.on('keydown', function (e) {
        if (e.keyCode == 9) {
            e.preventDefault();
            var indent = '    ';
            var start = this.selectionStart;
            var end = this.selectionEnd;
            var selected = window.getSelection().toString();
            selected = indent + selected.replace(/\n/g, '\n' + indent);
            this.value = this.value.substring(0, start) + selected
                + this.value.substring(end);
            this.setSelectionRange(start + indent.length, start
                + selected.length);
        }
    });


    if (this.layout) {
        this.layout.append(this.html);
    } else {
        this.html.addClass(this.options.className);
        this.html.addClass(this.options.size);
    };;
};
textarea.prototype = {
    constructor: this,
    render: function () {
        return this.html;
    },
    set: function (data, doChange) {
        this.element.val(data);
        if (doChange) {
            this.change();
        };
    },
    getValue: function () {
        var value = this.element.val();
        if (value == this.options.placeholder) {
            value = '';
        };
        return value;
    },
    setData: function (data) {
        this.set(data);
    },
    get: function (callBack) {
        var value = this.change();
        var result = this.getValue();
        if (!value) {
            callBack(false, result);
        } else {
            callBack(value);
        };
    },
    readonly: function (isReadonly) {
        var _isReadonly = null;
        if (jude.type(isReadonly, 'function')) {
            _isReadonly = isReadonly();
        } else {
            _isReadonly = !!isReadonly;
        };
        this.element.attr('readonly', _isReadonly);
        if (_isReadonly) {
            this.html.addClass('readonly');
        } else {
            this.html.removeClass('readonly');
        };
    },
    change: function () {
        var value = this.getValue();
        var _this = this;
        var status = null;
        if ($.inArray("notNull", _this.options.jude.rules) == -1 && value == '') {
            if (this.layout) {
                this.layout.hideError();
            };
            return false;
        };
        if (_this.options.jude.rules) {
            for (var i = 0; i < _this.options.jude.rules.length; i++) {
                var item = _this.options.jude.rules[i];
                if (!jude[item].perform(value)) {
                    status = (_this.options.jude.name || '') + jude[item].text;
                    break;
                };
            }
        };
        if (this.layout) {
            if (status) {
                this.layout.showError(status);
                return status;
            } else {
                this.layout.hideError();
                return false;
            }
        } else {
            if (status) {
                return status;
            } else {
                return false;
            }
        };
    },
    placeHolder: function () {
        var _this = this;
        if (!!(this.options.placeholder) && !('placeholder' in document.createElement('input'))) {
            this.element.val(this.element.attr('placeholder', _this.options.placeholder));
            this.element.val(this.element.attr('placeholder')).addClass('ie-placeholder');
            this.element.bind('focus', function () {
                if (_this.element.val() == _this.element.attr('placeholder')) {
                    _this.element.val("").removeClass('ie-placeholder');
                }
            }).bind('blur', function () {
                if (_this.element.val().length == 0) {
                    _this.element.val(_this.element.attr('placeholder')).addClass('ie-placeholder');
                }
            })
        } else {
            this.element.attr('placeholder', _this.options.placeholder);
        }
    }
};
module.exports = function (options, layout) {
    return new textarea(options, layout);
}
